﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Model
{
    [Serializable()]
    public class RBangLuongBO : Csla.BusinessBase<RBangLuongBO>
    {
        #region Business Properties

        private int? _BangLuongID = null;
        public int? BangLuongID
        {
            get { return this._BangLuongID; }
            set { this._BangLuongID = value; PropertyHasChanged("BangLuongID"); }
        }

        private int? _ThangTinhLuongID = null;
        public int? ThangTinhLuongID
        {
            get { return this._ThangTinhLuongID; }
            set { this._ThangTinhLuongID = value; PropertyHasChanged("ThangTinhLuongID"); }
        }

        private int? _Nam = null;
        public int? Nam
        {
            get { return this._Nam; }
            set { this._Nam = value; PropertyHasChanged("Nam"); }
        }

        private int? _NhanVienID = null;
        public int? NhanVienID
        {
            get { return this._NhanVienID; }
            set { this._NhanVienID = value; PropertyHasChanged("NhanVienID"); }
        }

        private int? _PhongBanID = null;
        public int? PhongBanID
        {
            get { return this._PhongBanID; }
            set { this._PhongBanID = value; PropertyHasChanged("PhongBanID"); }
        }

        private int? _ChucVuID = null;
        public int? ChucVuID
        {
            get { return this._ChucVuID; }
            set { this._ChucVuID = value; PropertyHasChanged("ChucVuID"); }
        }

        private double? _Luong = 0;
        public double? Luong
        {
            get
            {
                if (this._Luong != null)
                    if (this._Luong == 0)
                        return null;

                return this._Luong;
            }
            set { this._Luong = value; PropertyHasChanged("Luong"); }
        }

        private double? _PhuCap = 0;
        public double? PhuCap
        {
            get
            {
                if (this._PhuCap != null)
                    if (this._PhuCap == 0)
                        return null;

                return this._PhuCap;
            }
            set { this._PhuCap = value; PropertyHasChanged("PhuCap"); }
        }

        private double? _PhuCapBHXH = 0;
        public double? PhuCapBHXH
        {
            get
            {
                if (this._PhuCapBHXH != null)
                    if (this._PhuCapBHXH == 0)
                        return null;

                return this._PhuCapBHXH;
            }
            set { this._PhuCapBHXH = value; PropertyHasChanged("PhuCapBHXH"); }
        }

        private double? _ThuNhapKhac = 0;
        public double? ThuNhapKhac
        {
            get
            {
                if (this._ThuNhapKhac != null)
                    if (this._ThuNhapKhac == 0)
                        return null;

                return this._ThuNhapKhac;
            }
            set { this._ThuNhapKhac = value; PropertyHasChanged("ThuNhapKhac"); }
        }

        private double? _Tong = 0;
        public double? Tong
        {
            get
            {
                if (this._Tong != null)
                    if (this._Tong == 0)
                        return null;

                return this._Tong;
            }
            set { this._Tong = value; PropertyHasChanged("Tong"); }
        }

        private double? _TamUng = 0;
        public double? TamUng
        {
            get
            {
                if (this._TamUng != null)
                    if (this._TamUng == 0)
                        return null;

                return this._TamUng;
            }
            set { this._TamUng = value; PropertyHasChanged("TamUng"); }
        }

        private double? _BHXH = 0;
        public double? BHXH
        {
            get
            {
                if (this._BHXH != null)
                    if (this._BHXH == 0)
                        return null;

                return this._BHXH;
            }
            set { this._BHXH = value; PropertyHasChanged("BHXH"); }
        }

        private double? _BHYT = 0;
        public double? BHYT
        {
            get
            {
                if (this._BHYT != null)
                    if (this._BHYT == 0)
                        return null;

                return this._BHYT;
            }
            set { this._BHYT = value; PropertyHasChanged("BHYT"); }
        }

        private double? _BHTN = 0;
        public double? BHTN
        {
            get
            {
                if (this._BHTN != null)
                    if (this._BHTN == 0)
                        return null;

                return this._BHTN;
            }
            set { this._BHTN = value; PropertyHasChanged("BHTN"); }
        }

        private double? _Khac = 0;
        public double? Khac
        {
            get
            {
                if (this._Khac != null)
                    if (this._Khac == 0)
                        return null;

                return this._Khac;
            }
            set { this._Khac = value; PropertyHasChanged("Khac"); }
        }

        private double? _ThueTNCN = 0;
        public double? ThueTNCN
        {
            get
            {
                if (this._ThueTNCN != null)
                    if (this._ThueTNCN == 0)
                        return null;

                return this._ThueTNCN;
            }
            set { this._ThueTNCN = value; PropertyHasChanged("ThueTNCN"); }
        }

        private double? _ThucNhan = 0;
        public double? ThucNhan
        {
            get
            {
                if (this._ThucNhan != null)
                    if (this._ThucNhan == 0)
                        return null;

                return this._ThucNhan;
            }
            set { this._ThucNhan = value; PropertyHasChanged("ThucNhan"); }
        }
        private string _ThangLuong = null;
        public string ThangLuong
        {
            get { return this._ThangLuong; }
            set { this._ThangLuong = value; }
        }

        private string _HoTen = null;
        public string HoTen
        {
            get { return this._HoTen; }
            set { this._HoTen = value; }
        }

        private string _GioiTinh = null;
        public string GioiTinh
        {
            get { return this._GioiTinh; }
            set { this._GioiTinh = value; }
        }
        private int? _USRC = Session.LoginID;
        public int? USRC
        {
            get { return this._USRC; }
            set { this._USRC = value; PropertyHasChanged("USRC"); }
        }

        private DateTime? _DATC = DateTime.MaxValue;
        public DateTime? DATC
        {
            get { return this._DATC; }
            set { this._DATC = value; PropertyHasChanged("DATC"); }
        }

        private int? _USRM = null;
        public int? USRM
        {
            get { return this._USRM; }
            set { this._USRM = value; PropertyHasChanged("USRM"); }
        }

        private DateTime? _DATM = null;
        public DateTime? DATM
        {
            get { return this._DATM; }
            set { this._DATM = value; PropertyHasChanged("DATM"); }
        }

        private bool? _DELE = false;
        public bool? DELE
        {
            get { return this._DELE; }
            set { this._DELE = value; PropertyHasChanged("DELE"); }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; }
        }

        private string _MaNhanVien = null;
        public string MaNhanVien
        {
            get { return this._MaNhanVien; }
            set { this._MaNhanVien = value; }
        }

        private string _Ho = null;
        public string Ho
        {
            get { return this._Ho; }
            set { this._Ho = value; }
        }

        private string _Ten = null;
        public string Ten
        {
            get { return this._Ten; }
            set { this._Ten = value; }
        }

        private bool? _Nu = false;
        public bool? Nu
        {
            get { return this._Nu; }
            set { this._Nu = value; }
        }

        private string _PhongBan = null;
        public string PhongBan
        {
            get { return this._PhongBan; }
            set { this._PhongBan = value; }
        }

        private string _ChucVu = null;
        public string ChucVu
        {
            get { return this._ChucVu; }
            set { this._ChucVu = value; }
        }

        protected override object GetIdValue()
        {
            return _BangLuongID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newBangLuongID = -1;
        private RBangLuongBO()
        {
            _BangLuongID = newBangLuongID--;
        }

        public RBangLuongBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public RBangLuongBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static RBangLuongBO NewRootRBangLuongBO()
        {
            return DataPortal.Create<RBangLuongBO>(new RootCriteria());
        }

        internal static RBangLuongBO NewChildRBangLuongBO()
        {
            return DataPortal.Create<RBangLuongBO>(new ChildCriteria());
        }

        public static RBangLuongBO GetRootRBangLuongBO(int _BangLuongID)
        {
            return DataPortal.Fetch<RBangLuongBO>(new RootCriteria(_BangLuongID));
        }

        public static RBangLuongBO GetRootRBangLuongBOByWhere(string where)
        {
            return DataPortal.Fetch<RBangLuongBO>(where);
        }

        internal static RBangLuongBO GetChildRBangLuongBO(SqlDataReader dataReader)
        {
            return new RBangLuongBO(dataReader);
        }

        public static void DeleteRBangLuongBO(int _BangLuongID)
        {
            DataPortal.Delete(new RootCriteria(_BangLuongID));
        }

        public static void DeleteRBangLuongBO(int _BangLuongID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_BangLuongID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int BangLuongID;
            public bool directDelete = false;
            public RootCriteria(int _BangLuongID)
            {
                this.BangLuongID = _BangLuongID;
                this.directDelete = false;
            }

            public RootCriteria(int _BangLuongID, bool directDelete)
            {
                this.BangLuongID = _BangLuongID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int BangLuongID;
            public bool directDelete = false;
            public ChildCriteria(int _BangLuongID)
            {
                this.BangLuongID = _BangLuongID;
                this.directDelete = false;
            }

            public ChildCriteria(int _BangLuongID, bool directDelete)
            {
                this.BangLuongID = _BangLuongID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
        }

        private void This_Fetch(string where)
        {
        }

        private void Fetch(SqlDataReader dataReader)
        {
        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_BangLuongID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {

        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        public void CopyFromBangLuongBO(BangLuongBO blBO)
        {

            this.BangLuongID = blBO.BangLuongID;
            ThangTinhLuongID = blBO.ThangTinhLuongID;
            Nam = blBO.Nam;
            ThangLuong = "Tháng " + blBO.ThangTinhLuongID.ToString() + " năm " + blBO.Nam.ToString();
            NhanVienID = blBO.NhanVienID;
            MaNhanVien = blBO.MaNhanVien;
            HoTen = blBO.Ho + " " + blBO.Ten;
            PhongBanID = blBO.PhongBanID;
            ChucVuID = blBO.ChucVuID;
            Luong = blBO.Luong;
            PhuCap = blBO.PhuCap;
            PhuCapBHXH = blBO.PhuCapBHXH;
            ThuNhapKhac = blBO.ThuNhapKhac;
            Tong = blBO.Tong;
            TamUng = blBO.TamUng;
            BHXH = blBO.BHXH;
            BHYT = blBO.BHYT;
            BHTN = blBO.BHTN;
            Khac = blBO.Khac;
            ThueTNCN = blBO.ThueTNCN;
            ThucNhan = blBO.ThucNhan;
            GioiTinh = (StaticUtil.GetBool(blBO.Nu) ? "Nữ" : "Nam");
        }

        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_BangLuongID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
